package ui;
import core.GameManager;
import core.GameObjectFactory;
import model.Bullet;
import model.Enemy;
import observer.ScoreDisplay;
//import observer.ScoreObserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class GamePanel extends JPanel {
    private List<Enemy> enemies = new ArrayList<>();
    private List<Bullet> bullets = new ArrayList<>();
    private Timer gameTimer;
    private JLabel scoreLabel;
    private GameManager gameManager;
    private boolean isGameOver = false;
    // 移除重复的类定义
    private int difficulty;
    private ShootingGame shootingGame;

    public GamePanel(ShootingGame shootingGame, int difficulty) {
        this.shootingGame = shootingGame;
        this.difficulty = difficulty;
        gameManager = GameManager.getInstance();
        scoreLabel = new JLabel("得分: " + gameManager.getScore());
        scoreLabel.setFont(new Font("宋体", Font.BOLD, 18));
        scoreLabel.setForeground(Color.RED);
        add(scoreLabel);

        int initialEnemyCount = 5 + difficulty * 2;
        for (int i = 0; i < initialEnemyCount; i++) {
            enemies.add(GameObjectFactory.createEnemy());
        }

        gameTimer = new Timer(50 - difficulty * 10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateGame();
                repaint();
            }
        });
        gameTimer.start();

        addFunctionButtons();

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                int x = evt.getX();
                int y = evt.getY();
                Bullet bullet = GameObjectFactory.createBullet(x, y);
                bullets.add(bullet);
            }
        });
    }
    private void addFunctionButtons() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.setBackground(Color.LIGHT_GRAY); // 设置按钮面板背景颜色

        JButton returnButton = new JButton("返回首页");
        returnButton.setBackground(Color.ORANGE); // 设置按钮背景颜色
        returnButton.setForeground(Color.WHITE); // 设置按钮文字颜色
        returnButton.setFont(new Font("宋体", Font.BOLD, 14)); // 设置按钮字体
        returnButton.addActionListener(e -> returnToMainMenu());

        JButton restartButton = new JButton("重新开始");
        restartButton.setBackground(Color.RED); // 设置按钮背景颜色
        restartButton.setForeground(Color.WHITE); // 设置按钮文字颜色
        restartButton.setFont(new Font("宋体", Font.BOLD, 14)); // 设置按钮字体
        restartButton.addActionListener(e -> restartGame());

        buttonPanel.add(returnButton);
        buttonPanel.add(restartButton);

        add(buttonPanel, BorderLayout.WEST);
    }
    
    private void returnToMainMenu() {
        gameTimer.stop();
        // 重置游戏状态
        enemies.clear();
        bullets.clear();
        gameManager.resetScore();
        isGameOver = false;
        if (shootingGame != null) {
            shootingGame.showMainMenu();
        }
    }
    
    private void restartGame() {
        gameTimer.stop();
        // 重新初始化游戏逻辑
        enemies.clear();
        bullets.clear();
        gameManager.resetScore(); // 需要在 GameManager 中添加 resetScore 方法
        isGameOver = false;
        // 根据难度重新初始化敌人数量
        int initialEnemyCount = 5 + difficulty * 2;
        for (int i = 0; i < initialEnemyCount; i++) {
            enemies.add(GameObjectFactory.createEnemy());
        }
        gameTimer.start();
    }
    
    // 修改 updateGame 方法，在游戏结束时显示结束页面
    private void updateGame() {
        if (isGameOver) return;
    
        // 移动子弹
        for (Bullet bullet : bullets) {
            bullet.move();
        }
    
        // 移动敌人
        for (Enemy enemy : enemies) {
            enemy.move();
        }
    
        // 检查子弹与敌人的碰撞
        for (int i = bullets.size() - 1; i >= 0; i--) {
            Bullet bullet = bullets.get(i);
            // 使用 getter 方法获取坐标
            Rectangle bulletRect = new Rectangle(bullet.getX(), bullet.getY(), 5, 10);
    
            // 使用 getter 方法获取坐标
            if (bullet.getY() < 0) { 
                bullets.remove(i);
            } else {
                for (int j = enemies.size() - 1; j >= 0; j--) {
                    Enemy enemy = enemies.get(j);
                    // 修改为使用 getter 方法
                    Rectangle enemyRect = new Rectangle(enemy.getX(), enemy.getY(), 50, 50);
    
                    if (bulletRect.intersects(enemyRect)) {
                        bullets.remove(i);
                        enemies.remove(j);
                        gameManager.increaseScore();
                        ScoreDisplay scoreDisplay = new ScoreDisplay(scoreLabel);
                        scoreDisplay.updateScore(gameManager.getScore());
                    
                        break;
                    }
                }
            }
        }
    
        // 检查敌人是否到达屏幕底部
        for (Enemy enemy : enemies) {
            if (enemy.getY() > getHeight()) {
                isGameOver = true;
                gameTimer.stop();
                showGameOverScreen();
            }
        }
    
        // 随机生成新的敌人
        if (enemies.size() < 10) {
            enemies.add(GameObjectFactory.createEnemy());
        }
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制敌人，修改为使用 getter 方法
        for (Enemy enemy : enemies) {
            g.fillOval(enemy.getX(), enemy.getY(), 50, 50);
        }
    
        // 绘制子弹，使用 getter 方法获取坐标
        for (Bullet bullet : bullets) { 
            g.fillRect(bullet.getX(), bullet.getY(), 5, 10);
        }
    }
    
    private void showGameOverScreen() {
        JPanel gameOverPanel = new JPanel();
        gameOverPanel.setLayout(new BoxLayout(gameOverPanel, BoxLayout.Y_AXIS));
        gameOverPanel.setBackground(Color.DARK_GRAY); // 设置游戏结束面板背景颜色
    
        JLabel gameOverLabel = new JLabel("游戏结束！得分: " + gameManager.getScore());
        gameOverLabel.setFont(new Font("宋体", Font.BOLD, 24)); // 设置游戏结束标签字体
        gameOverLabel.setForeground(Color.YELLOW); // 设置游戏结束标签文字颜色
        gameOverLabel.setAlignmentX(Component.CENTER_ALIGNMENT); // 居中对齐
    
        JButton returnButton = new JButton("返回首页");
        returnButton.setBackground(Color.ORANGE); // 设置按钮背景颜色
        returnButton.setForeground(Color.WHITE); // 设置按钮文字颜色
        returnButton.setFont(new Font("宋体", Font.BOLD, 14)); // 设置按钮字体
        returnButton.addActionListener(e -> returnToMainMenu());
    
        JButton exitButton = new JButton("退出游戏");
        exitButton.setBackground(Color.RED); // 设置按钮背景颜色
        exitButton.setForeground(Color.WHITE); // 设置按钮文字颜色
        exitButton.setFont(new Font("宋体", Font.BOLD, 16)); // 设置按钮字体
        exitButton.addActionListener(e -> System.exit(0));
    
        gameOverPanel.add(Box.createVerticalGlue()); // 添加垂直间距
        gameOverPanel.add(gameOverLabel);
        gameOverPanel.add(Box.createVerticalStrut(20)); // 添加垂直间距
        gameOverPanel.add(returnButton);
        gameOverPanel.add(Box.createVerticalStrut(10)); // 添加垂直间距
        gameOverPanel.add(exitButton);
        gameOverPanel.add(Box.createVerticalGlue()); // 添加垂直间距
    
        JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
        if (frame != null) {
            frame.setContentPane(gameOverPanel);
        }
        revalidate();
        repaint();
    }
    }
